//***************************************************************************
//*
//*	File:			dswl_genericdocument.cpp
//*	Description:	Wrapper for IGenericDocument interface
//*
//***************************************************************************

//
//--------------------------------------------------------------- PRECOMPILED
//

#include "stdafx.h"

//
//--------------------------------------------------- DECLARATION DEPENDENCIES
//

//
//--------------------------------------------------------------- DECLARATION
//

#include "../Inc/dswl_genericdocument.h"

//
//--------------------------------------------------- DEFINITION DEPENDENCIES
//

//
//-------------------------------------------------------------- PREPROCESSOR
//

#ifdef	_DEBUG
#define new DEBUG_NEW
#undef	THIS_FILE
static	char THIS_FILE[] = __FILE__;
#endif

//
//---------------------------------------------------------------- DEFINITION
//

/*
static void g_TestCasts()
{
	// test _safe_ conversions
	IGenericDocument* pITest;
	CIGenericDocument test	 ( pITest );
	CIGenericDocument test2 = pITest ;

	test = test2;
	test = pITest;

	#ifdef _DSWL_SWITCH_USE_AUTOCAST

	// test _critical_ conversions
	pITest = test;
	CComPtr< IGenericDocument > tmplTest = test;

	#endif
}
*/

//
//---------------------------------------------------------------------------------------------------
//*************************************     CON/DESTRUCTION     *************************************
//---------------------------------------------------------------------------------------------------
//

//***************************************************************************************************
//**																					  Constructor
//***************************************************************************************************
//**	@DOC		CONSTRUCTION
//**	@MFUNC		Default constructor
//**	@PARM		[in] A pointer to the interface to attach to
//**	@END
//***************************************************************************************************
//inline
CIGenericDocument::CIGenericDocument( IGenericDocument* a_pIGenericDocument /*= NULL*/ )
{
	this->m_pIGenericDocument = a_pIGenericDocument;
}

//***************************************************************************************************
//**																					   Destructor
//***************************************************************************************************
//**	@DOC		CONSTRUCTION
//**	@MFUNC		Default destructor
//**	@END
//***************************************************************************************************
//inline
CIGenericDocument::~CIGenericDocument()
{
	this->m_pIGenericDocument = NULL;	// explicitly release the interface now
}

//
//---------------------------------------------------------------------------------------------------
//***************************************     PROPERTIES     ****************************************
//---------------------------------------------------------------------------------------------------
//

//***************************************************************************************************
//**																				   GetApplication 
//***************************************************************************************************
//**	@DOC		PROPERTIES
//**	@MFUNC		Determines the application this document belongs to
//**	@RDESC		A pointer to the application's interface
//**	@END
//***************************************************************************************************
//inline
CIApplication CIGenericDocument::GetApplication( void ) const 
{
	//
	//	CHECK VALIDITY
	//
	if ( FALSE == this->IsValid() )
	{
		return NULL;
	}

	//
	//	DELEGATE
	//
	IDispatch*	pIDispatch = NULL;

	DSWL_VERIFY( this->m_pIGenericDocument->get_Application( &pIDispatch ) );

	//
	//	QUERY INTERFACE
	//
	CComQIPtr< IApplication, &IID_IApplication > pIApplication ( pIDispatch );	

	//
	//	REPELASE DISPATCH INTERFACE
	//
	DSWL_RELEASE_EX( pIDispatch, CIGenericDocument::GetApplication );

	//
	//	RETURN QUERIED INTERFACE
	//
	return pIApplication.p;												
}

//***************************************************************************************************
//**																				  GetActiveWindow
//***************************************************************************************************
//**	@DOC		PROPERTIES
//**	@MFUNC		Determines the document's active window
//**	@RDESC		A pointer to the windows' interface
//**	@END
//***************************************************************************************************
//inline
CIGenericWindow CIGenericDocument::GetActiveWindow( void ) const 
{
	//
	//	CHECK VALIDITY
	//
	if ( FALSE == this->IsValid() )
	{
		return NULL;
	}
	
	//
	//	DELEGATE
	//
	IDispatch*	pIDispatch = NULL;

	DSWL_VERIFY( this->m_pIGenericDocument->get_ActiveWindow( &pIDispatch ) );

	//
	//	QUERY INTERFACE
	//
	CComQIPtr< IGenericWindow, &IID_IGenericWindow > pIGenericWindow ( pIDispatch );	

	//
	//	REPELASE DISPATCH INTERFACE
	//
	DSWL_RELEASE_EX( pIDispatch, CIGenericDocument::GetActiveWindow );

	//
	//	RETURN QUERIED INTERFACE
	//
	return pIGenericWindow.p;												
}

//***************************************************************************************************
//**																					   GetWindows
//***************************************************************************************************
//**	@DOC		PROPERTIES
//**	@MFUNC		Determines the windows displaying this document
//**	@RDESC		A pointer to the windows' interface
//**	@END
//***************************************************************************************************
//inline
CIWindows CIGenericDocument::GetWindows( void ) const 
{
	//
	//	CHECK VALIDITY
	//
	if ( FALSE == this->IsValid() )
	{
		return NULL;
	}
	
	//
	//	DELEGATE
	//
	IDispatch*	pIDispatch = NULL;

	DSWL_VERIFY( this->m_pIGenericDocument->get_Windows( &pIDispatch ) );

	//
	//	QUERY INTERFACE
	//
	CComQIPtr< IWindows, &IID_IWindows > pIWindows ( pIDispatch );	

	//
	//	REPELASE DISPATCH INTERFACE
	//
	DSWL_RELEASE_EX( pIDispatch, CIGenericDocument::GetWindows );

	//
	//	RETURN QUERIED INTERFACE
	//
	return pIWindows.p;												
}

//***************************************************************************************************
//**																					  GetFullName
//***************************************************************************************************
//**	@DOC		PROPERTIES
//**	@MFUNC		Returns the document's full name
//**	@RDESC		The full name
//**	@END
//***************************************************************************************************
//inline
CString CIGenericDocument::GetFullName( void ) const 
{
	//
	//	CHECK VALIDITY
	//
	if ( FALSE == this->IsValid() )
	{
		return _T( "" );
	}

	//
	//	DELEGATE
	//
	CComBSTR bszFullName( _T( "" ) );

	DSWL_VERIFY( this->m_pIGenericDocument->get_FullName( &bszFullName ) );

	return bszFullName;
}

//***************************************************************************************************
//**																						  GetName
//***************************************************************************************************
//**	@DOC		PROPERTIES
//**	@MFUNC		Returns the document's name
//**	@RDESC		The name
//**	@END
//***************************************************************************************************
//inline
CString CIGenericDocument::GetName( void ) const 
{
	//
	//	CHECK VALIDITY
	//
	if ( FALSE == this->IsValid() )
	{
		return _T( "" );
	}

	//
	//	DELEGATE
	//
	CComBSTR bszName( _T( "" ) );

	DSWL_VERIFY( this->m_pIGenericDocument->get_Name( &bszName ) );

	return bszName;
}

//***************************************************************************************************
//**																						  GetPath
//***************************************************************************************************
//**	@DOC		PROPERTIES
//**	@MFUNC		Returns the document's path
//**	@RDESC		The path
//**	@END
//***************************************************************************************************
//inline
CString CIGenericDocument::GetPath( void ) const 
{
	//
	//	CHECK VALIDITY
	//
	if ( FALSE == this->IsValid() )
	{
		return _T( "" );
	}

	//
	//	DELEGATE
	//
	CComBSTR bszPath( _T( "" ) );

	DSWL_VERIFY( this->m_pIGenericDocument->get_Path( &bszPath ) );

	return bszPath;
}

//***************************************************************************************************
//**																						 IsActive
//***************************************************************************************************
//**	@DOC		PROPERTIES
//**	@MFUNC		Determines whether the document is active
//**	@RDESC		<t TRUE> if the document is active, <t FALSE> othwerwise.
//**	@END
//***************************************************************************************************
//inline
BOOL CIGenericDocument::IsActive( void ) const 
{
	ASSERT( TRUE == this->IsValid() );

	//
	//	DELEGATE
	//
	VARIANT_BOOL varResult( VARIANT_FALSE );

	DSWL_VERIFY( this->m_pIGenericDocument->get_Active( &varResult ) );

	return varResult;
}

//***************************************************************************************************
//**																						  IsSaved
//***************************************************************************************************
//**	@DOC		PROPERTIES
//**	@MFUNC		Determines whether the document is saved
//**	@RDESC		<t TRUE> if the document is saved, <t FALSE> othwerwise.
//**	@END
//***************************************************************************************************
//inline
BOOL CIGenericDocument::IsSaved( void ) const 
{
	ASSERT( TRUE == this->IsValid() );

	//
	//	DELEGATE
	//
	VARIANT_BOOL varResult( VARIANT_FALSE );

	DSWL_VERIFY( this->m_pIGenericDocument->get_Saved( &varResult ) );

	return varResult;
}

//***************************************************************************************************
//**																					   IsReadOnly
//***************************************************************************************************
//**	@DOC		PROPERTIES
//**	@MFUNC		Determines whether the document is read-only
//**	@RDESC		<t TRUE> if the document is read-only, <t FALSE> othwerwise.
//**	@END
//***************************************************************************************************
//inline
BOOL CIGenericDocument::IsReadOnly( void ) const 
{
	ASSERT( TRUE == this->IsValid() );

	//
	//	DELEGATE
	//
	VARIANT_BOOL varResult( VARIANT_FALSE );

	DSWL_VERIFY( this->m_pIGenericDocument->get_ReadOnly( &varResult ) );

	return varResult;
}

//***************************************************************************************************
//**																						SetActive
//***************************************************************************************************
//**	@DOC		PROPERTIES
//**	@MFUNC		Activates or deactivates the document
//**	@PARM		[in] A flag indicating whether to activate or deactivate the document		
//**	@END
//***************************************************************************************************
//inline
void CIGenericDocument::SetActive( BOOL a_bEnable ) const 
{
	ASSERT( TRUE == this->IsValid() );

	//
	//	DELEGATE
	//
	VARIANT_BOOL varEnable( a_bEnable );

	DSWL_VERIFY( this->m_pIGenericDocument->put_Active( varEnable ) );
}

//***************************************************************************************************
//**																					  SetReadOnly
//***************************************************************************************************
//**	@DOC		PROPERTIES
//**	@MFUNC		Sets or removes the read-only flag from the document
//**	@PARM		[in] A flag indicating whether to enable or disable read-only mode
//**	@END
//***************************************************************************************************
//inline
void CIGenericDocument::SetReadOnly( BOOL a_bEnable ) const 
{
	ASSERT( TRUE == this->IsValid() );

	//
	//	DELEGATE
	//
	VARIANT_BOOL varEnable( a_bEnable );

	DSWL_VERIFY( this->m_pIGenericDocument->put_ReadOnly( varEnable ) );
}

//
//---------------------------------------------------------------------------------------------------
//***************************************     INFORMATION     ***************************************
//---------------------------------------------------------------------------------------------------
//

//***************************************************************************************************
//**																						   IsNull
//***************************************************************************************************
//**	@DOC		INFORMATION
//**	@MFUNC		Determines whether the internal pointer is <t NULL>
//**	@RDESC		<t TRUE> if the internal pointer is <t NULL>, <t FALSE> otherwise.
//**	@END
//***************************************************************************************************
//inline
BOOL CIGenericDocument::IsNull( void ) const
{
	return ( NULL == this->m_pIGenericDocument.p );
}

//***************************************************************************************************
//**																						  IsValid
//***************************************************************************************************
//**	@DOC		INFORMATION
//**	@MFUNC		Determines whether the internal pointer differs from <t NULL>
//**	@RDESC		<t TRUE> if the internal pointer differs from <t NULL>, <t FALSE> otherwise.
//**	@END
//***************************************************************************************************
//inline
BOOL CIGenericDocument::IsValid( void ) const
{
	return ( NULL != this->m_pIGenericDocument.p );
}

//
//---------------------------------------------------------------------------------------------------
//**************************************     MODIFICATION     ***************************************
//---------------------------------------------------------------------------------------------------
//

//***************************************************************************************************
//**																						NewWindow
//***************************************************************************************************
//**	@DOC		MODIFICATION
//**	@MFUNC		Opens a new window dispalying the document
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
CIGenericWindow CIGenericDocument::NewWindow( void ) 
{
	ASSERT( this->IsValid() );

	IDispatch* pIDispatch = NULL;

	DSWL_VERIFY( this->m_pIGenericDocument->NewWindow( &pIDispatch ) );

	//
	//	QUERY INTERFACE
	//
	CComQIPtr< IGenericWindow, &IID_IGenericWindow > pIGenericWindow ( pIDispatch );	

	//
	//	REPELASE DISPATCH INTERFACE
	//
	DSWL_RELEASE_EX( pIDispatch, CIGenericDocument::NewWindow );

	//
	//	RETURN QUERIED INTERFACE
	//
	return pIGenericWindow.p;												
}

//***************************************************************************************************
//**																						 PrintOut
//***************************************************************************************************
//**	@DOC		MODIFICATION
//**	@MFUNC		Prints the document
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
BOOL CIGenericDocument::PrintOut( void ) 
{
	ASSERT( this->IsValid() );

	VARIANT_BOOL varResult( VARIANT_FALSE );

	DSWL_VERIFY( this->m_pIGenericDocument->PrintOut( &varResult ) );

	return varResult;
}

//***************************************************************************************************
//**																							 Save
//***************************************************************************************************
//**	@DOC		MODIFICATION
//**	@MFUNC		Saves the document
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
DsSaveStatus CIGenericDocument::Save( const CString& a_strFile, BOOL a_bPrompt ) 
{
	ASSERT( this->IsValid() );

	CComVariant		varFile		( a_strFile	);
	CComVariant		varPrompt	( a_bPrompt	);
	DsSaveStatus	enuResult	;

	DSWL_VERIFY( this->m_pIGenericDocument->Save( varFile, varPrompt, &enuResult ) );

	return enuResult;
}

//***************************************************************************************************
//**																							Close
//***************************************************************************************************
//**	@DOC		MODIFICATION
//**	@MFUNC		Closes the document
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
DsSaveStatus CIGenericDocument::Close( BOOL a_bSave ) 
{
	ASSERT( this->IsValid() );

	CComVariant		varSave		( a_bSave	);
	DsSaveStatus	enuResult	;

	/*DSWL_VERIFY(*/ this->m_pIGenericDocument->Close( varSave, &enuResult ) /*)*/;						// will fail when there is no window displaying the document

	return enuResult;
}

//***************************************************************************************************
//**																							 Redo
//***************************************************************************************************
//**	@DOC		MODIFICATION
//**	@MFUNC		Re-does the latest undo
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
BOOL CIGenericDocument::Redo( void ) 
{
	ASSERT( this->IsValid() );

	VARIANT_BOOL varResult = VARIANT_FALSE;

	DSWL_VERIFY( this->m_pIGenericDocument->Redo(  &varResult ) );

	return varResult;
}

//***************************************************************************************************
//**																							 Undo
//***************************************************************************************************
//**	@DOC		MODIFICATION
//**	@MFUNC		Un-does the latest modification
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
BOOL CIGenericDocument::Undo( void ) 
{
	ASSERT( this->IsValid() );

	VARIANT_BOOL varResult = VARIANT_FALSE;

	DSWL_VERIFY( this->m_pIGenericDocument->Undo(  &varResult ) );

	return varResult;
}

//
//---------------------------------------------------------------------------------------------------
//****************************************     OPERATORS     ****************************************
//---------------------------------------------------------------------------------------------------
//

//***************************************************************************************************
//**																					  operator ==
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Comparison operator
//**	@PARM		[in] The generic document to compare to
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
bool CIGenericDocument::operator == ( const CIGenericDocument& a_Other ) const 
{
	return this->m_pIGenericDocument == a_Other.m_pIGenericDocument;
}

//***************************************************************************************************
//**																					  operator !=
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Comparison operator
//**	@PARM		[in] The generic document to compare to
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
bool CIGenericDocument::operator != ( const CIGenericDocument& a_Other ) const 
{
	return this->m_pIGenericDocument != a_Other.m_pIGenericDocument;
}

//***************************************************************************************************
//**																					  operator <=
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Comparison operator
//**	@PARM		[in] The generic document to compare to
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
bool CIGenericDocument::operator <= ( const CIGenericDocument& a_Other ) const 
{
	return this->m_pIGenericDocument.p <= a_Other.m_pIGenericDocument.p;
}

//***************************************************************************************************
//**																					  operator >=
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Comparison operator
//**	@PARM		[in] The generic document to compare to
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
bool CIGenericDocument::operator >= ( const CIGenericDocument& a_Other ) const 
{
	return this->m_pIGenericDocument.p >= a_Other.m_pIGenericDocument.p;
}

//***************************************************************************************************
//**																					   operator <
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Comparison operator
//**	@PARM		[in] The generic document to compare to
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
bool CIGenericDocument::operator < ( const CIGenericDocument& a_Other ) const 
{
	return this->m_pIGenericDocument.p < a_Other.m_pIGenericDocument.p;
}

//***************************************************************************************************
//**																					   operator >
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Comparison operator
//**	@PARM		[in] The generic document to compare to
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
bool CIGenericDocument::operator > ( const CIGenericDocument& a_Other ) const 
{
	return this->m_pIGenericDocument.p > a_Other.m_pIGenericDocument.p;
}

//***************************************************************************************************
//**																					   operator =
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Copy operator
//**	@PARM		[in] The generic document to attach to
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
CIGenericDocument& CIGenericDocument::operator = ( IGenericDocument* a_pIOther ) 
{
	this->m_pIGenericDocument = a_pIOther;

	return (*this);
}

//***************************************************************************************************
//**																					   operator *
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Dereference operator
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
IGenericDocument* CIGenericDocument::operator * ( void ) 
{
	return this->m_pIGenericDocument.p;
}

//***************************************************************************************************
//**																				 const operator *
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Dereference operator
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
const IGenericDocument* CIGenericDocument::operator * ( void ) const 
{
	return this->m_pIGenericDocument.p;
}

//***************************************************************************************************
//**																					  operator ->
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Member access operator
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
IGenericDocument* CIGenericDocument::operator -> ( void ) 
{
	return * (*this);
}

//***************************************************************************************************
//**																				const operator ->
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Member access operator
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
const IGenericDocument* CIGenericDocument::operator -> ( void ) const 
{
	return * (*this);
}

//---------------------------------------------------------------------------------------------------
//***************************************************************************************************
//---------------------------------------------------------------------------------------------------

#ifdef _DSWL_SWITCH_USE_AUTOCAST
#ifdef _DSWL_SWITCH_USE_UNSAFE_AUTOCAST

//***************************************************************************************************
//**																				 const operator *
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		TypeCast operator
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
CIGenericDocument::operator IGenericDocument* ( void ) const 
{
	return this->m_pIGenericDocument.p;
}

#else

//***************************************************************************************************
//**																					   operator *
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		TypeCast operator
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
CIGenericDocument::operator IGenericDocument* ( void ) 
{
	return this->m_pIGenericDocument.p;
}

//***************************************************************************************************
//**																				 const operator *
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		TypeCast operator
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
CIGenericDocument::operator const IGenericDocument* ( void ) const 
{
	return this->m_pIGenericDocument.p;
}

#endif
#endif


